//
//  UIViewController-Extension.swift
//  APP0517
//
//  Created by lishengbing on 2019/7/25.
//  Copyright © 2019年 Li Shengbing, (Shengbing.Li@partner.bmw.com). All rights reserved.
//

import UIKit

extension UIViewController {
    
    // handler: ((UIAlertAction) -> Void)? = nil
    
    /// show alert message
    ///
    /// - Parameters:
    ///   - leftHandler: callback left button event to outside.
    ///   - rightHandler: callback right button event to outside.
    ///   - mainTitle: main title
    ///   - message: message
    ///   - leftTitle: left button title
    ///   - rightTitle: right button title
   public func showAlertViewController(leftHandler: ((UIAlertAction) -> Void)? = nil, rightHandler: ((UIAlertAction) -> Void)? = nil, mainTitle: String? = nil, message: String? = nil, leftTitle: String? = nil, rightTitle: String? = nil) {
        
        let alertVc = UIAlertController(title: mainTitle ?? "\"请输入一个标题\"", message: message, preferredStyle: UIAlertController.Style.alert)
        let leftAction = UIAlertAction(title: leftTitle ?? "\"缺少一个左边按钮标题\"", style: .default, handler: leftHandler)
        let rightAction = UIAlertAction(title: rightTitle ?? "\"缺少一个右边按钮标题\"", style: .default, handler: rightHandler)
        if let leftText = leftTitle, !leftText.isEmpty {
            alertVc.addAction(leftAction)
        }
        if let rightText = rightTitle, !rightText.isEmpty {
            alertVc.addAction(rightAction)
        }
        present(alertVc, animated: true, completion: nil)
    }
    
    
    public enum HUDColorStyle {
        case white
        case black
        
        var bezelViewColor: UIColor {
            switch self {
            case .black:
                return UIColor.black
            case .white:
                return UIColor.white
            }
        }
        
        var textColor: UIColor {
            switch self {
            case .black:
                return UIColor.white
            case .white:
                return UIColor.black
            }
        }
    }
    /*
     /// https://www.jianshu.com/p/a933e5360d0b
     through the property to search warning code.
     hud.areDefaultMotionEffectsEnabled = true
     
     /// lishengbing modify 2019-5-26, otherwise show warning: close this line is no problem.
     /// Main Thread Checker: UI API called on a background thread
     /// [bezelView addMotionEffect:group];
     */
    /// show hud
    ///
    /// - Parameter title: show title message.
    public func showHud(title: String, style: HUDColorStyle? = .black, complete: (() -> Void)? = nil) {
        var s = style
        if s == nil {
            s = .black
        }
        let hud = MBProgressHUD.showAdded(to: self.view, animated: true)
        hud.mode = .text
        hud.label.text = title
        hud.label.textColor = s!.textColor
        hud.label.numberOfLines = 2
        hud.margin = 20
        hud.bezelView.style = .solidColor
        hud.bezelView.color = s!.bezelViewColor
        hud.hide(animated: true, afterDelay: 1.2)
        hud.completionBlock = {
            if complete != nil {
                complete!()
            }
        }
    }
}
